package com.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.test.entity.User;
import com.test.mapper.UserMapperPlus;
import com.test.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class UserMapperPlusTest {

    private SqlSession sqlSession;

    private UserMapperPlus userMapperPlus;

    @Before
    public void setUp() {
        // 创建测试表
//        DatabaseUtil.createTestTable();

        // 获取SqlSession和Mapper
        sqlSession = MyBatisUtil.getSqlSession();
        userMapperPlus = sqlSession.getMapper(UserMapperPlus.class);

        System.out.println("🚀 开始执行MyBatis CRUD测试...");
    }
    @Test
    public void insertUser(){
        User user = new User("张三123", "111@qq.com", 18);
        int num = userMapperPlus.insert(user);
        System.out.println("插入成功，条数是：" + num);
    }

    @Test
    public void queryList(){
        QueryWrapper<User>wrapper=new QueryWrapper();
        List<User> users = userMapperPlus.selectList(wrapper);
        users.forEach(bean ->{
            System.out.println(users);
        });
    }
    @Test
    public void update(){
        User user=new User(12,"李四","111@qq.com",18);
        userMapperPlus.updateById(user);
    }
    @Test
    public void update2(){
        UpdateWrapper<User>wrapper=new UpdateWrapper();
        wrapper.eq("id",13);
        wrapper.set("name","王五");

        User user=new User();
        userMapperPlus.update(user,wrapper);
    }

}
